package kr.hkit.powerjava.chapter22.dpt;

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

public class dptTest {
	public static void main(String[] args) {
		Map<Department, Department> dptList = new HashMap<Department, Department>();
		dptList.put(new Department(1, "Java"), new Department(1, "Java", 102));
		dptList.put(new Department(2, "DB"), new Department(2, "DB", 203));
		dptList.put(new Department(3, "UML"), new Department(3, "UML", 304));

		DepartmentMeneger dm = new DepartmentMeneger(dptList);

		int res = 0;
		Department valueDpt = null;
		Department keyDpt = null;

		while (res != 6) {
			try {
				res = Integer.parseInt(JOptionPane
						.showInputDialog("1.추가 2.삭제 3.수정 4.검색 5.출력 6.종료"));
			} catch (NumberFormatException e) {
				System.out.println("종료");
				res = 6;
			}
			switch (res) {
				case 1:
					valueDpt = inputDptInfo(true);
					keyDpt = new Department(valueDpt.getDptNo(),
							valueDpt.getDptName());
					dm.addItem(keyDpt, valueDpt);
					break;
				case 2:
					keyDpt = inputDptInfo(false);
					dm.delItem(keyDpt);
					break;
				case 3:
					valueDpt = inputDptInfo(true);
					keyDpt = new Department(valueDpt.getDptNo(),
							valueDpt.getDptName());
					dm.updateItem(keyDpt, valueDpt);
					break;
				case 4:
					keyDpt = inputDptInfo(false);
					dm.searchItems(keyDpt);
					break;
				case 5:
					dm.prnHeader();
					dm.prnItem();
					break;
			}
		}
	}

	private static Department inputDptInfo(boolean b) {
		if (b) {
			String dpt = JOptionPane.showInputDialog("과목번호 과목이름 강의실");
			StringTokenizer st = new StringTokenizer(dpt);
			int dptNo = Integer.parseInt(st.nextToken());
			String dptName = st.nextToken();
			int floor = Integer.parseInt(st.nextToken());
			return new Department(dptNo, dptName, floor);
		} else {
			String dpt = JOptionPane.showInputDialog("과목번호 과목이름");
			StringTokenizer st = new StringTokenizer(dpt);
			int dptNo = Integer.parseInt(st.nextToken());
			String dptName = st.nextToken();
			return new Department(dptNo, dptName);
		}
	}
}
